<?php
	if ((!defined("ADMIN-AREA")) || (!in_array(SHOWVIEW,$_SESSION['access']))) {
		die("No data");
	} else {
		unset($_SESSION['access']);
	}
	
	include_once "modul/funct_stat.php";
	
	if (ACT=="edit") {
		$sql1 = "SELECT * FROM tbl_content WHERE idx='".$_GET['kode']."' AND tipe='event' LIMIT 1";
		$qry1 = mysql_query($sql1);
		while ($row1 = mysql_fetch_object($qry1)) {
			$event = $row1->judul;
			$tanggal = explode('[break]',$row1->header_c);
			$tanggal1 = $tanggal[0];
			$tanggal2 = $tanggal[1];
			$event_deskripsi = $row1->isi;
			if (($row1->status)==1) {
				$pubx = "checked";
			};
		};
					
		$form_stat = "Edit";
	} else {
		$form_stat = "Input";
	}
	if ((ACT=="tambah") || (ACT=="edit")) {
?>
<form name="form1" method="post" action="<?php echo $path; ?>ver_event.php?show=<?php echo SHOW; ?>&showview=<?php echo SHOWVIEW; ?>" onsubmit="MM_validateForm('<?php echo $EVENT_TITLE; ?>','','R','<?php echo $EVENT_HEADER; ?>','','R','<?php echo $EVENT_DATE; ?>','','R');return document.MM_returnValue">
  <table width="450" border="0" align="center" cellpadding="4" cellspacing="2" class="content-arial">
    <tr valign="top">
      <td valign="middle" nowrap="nowrap"><div align="right"><?php echo $EVENT_TITLE; ?></div></td>
      <td valign="middle"><div align="center">:</div></td>
      <td valign="middle"><div align="left">
        <input name="nama_event" type="text" id="<?php echo $EVENT_TITLE; ?>" value="<?php echo $event; ?>" size="35" class="input-title" />
      </div></td>
    </tr>
    <tr valign="top"> 
      <td width="21%" nowrap="nowrap"> <div align="right"><?php echo $EVENT_CONTENT; ?></div></td>
      <td width="2%"> <div align="center">:</div></td>
      <td width="77%"> <div align="left"> 
          <textarea name="isi" cols="65" rows="20" id="<?php echo $EVENT_CONTENT; ?>"><?php echo $event_deskripsi; ?></textarea>
        </div></td>
    </tr>
    <tr valign="top"> 
      <td nowrap="nowrap"> <div align="right"><?php echo $EVENT_DATE_BEGIN; ?></div></td>
      <td> <div align="center">:</div></td>
      <td> <div align="left">
        <input name="tanggal1" type="text" id="<?php echo $EVENT_DATE_BEGIN; ?>" readonly="readonly" value="<?php echo $tanggal1; ?>" />
        <input name="tgl1" type="button" id="tgl1" value="..." />
</div></td>
    </tr>
    <tr valign="top">
      <td nowrap="nowrap"><div align="right"><?php echo $EVENT_DATE_END; ?></div></td>
      <td><div align="center">:</div></td>
      <td><div align="left">
        <input name="tanggal2" type="text" id="<?php echo $EVENT_DATE_END; ?>" readonly="readonly" value="<?php echo $tanggal2; ?>" />
        <input name="tgl2" type="button" id="tgl2" value="..." />
      </div></td>
    </tr>
    <tr> 
      <td nowrap="nowrap"> <div align="right"><?php echo $EVENT_PUBLISH; ?></div></td>
      <td> <div align="center">:</div></td>
      <td> <div align="left">
        <input name="publish" type="checkbox" id="publish" value="ya" <?php echo $pubx; ?> />
      <?php echo $DEV_YES; ?></div></td>
    </tr>
    <tr> 
      <td nowrap="nowrap"> <div align="right"></div></td>
      <td><div align="center"></div></td>
      <td><div align="left">
        <input type="submit" name="submit" id="submit" value="<?php echo $DEV_CTRL_SUBMIT; ?>" />
          <input type="reset" name="Reset" value="<?php echo $DEV_CTRL_RESET; ?>" />
        <input name="act_ver" type="hidden" id="act_ver" value="<?php echo strtolower($form_stat); ?>">
          <input name="kode" type="hidden" id="kode" value="<?php echo (int) $_GET['kode']; ?>">
        </div></td>
    </tr>
  </table>
</form>
<script type="text/javascript">
	var y4 = "([0-9]{4})";
	var y2 = "([0-9]{2})";
	var yi = -1;
	
	var M2 = "(0[1-9]|1[0-2])";
	var M1 = "([1-9]|1[0-2])";
	var Mi = -1;
	
	var d2 = "(0[1-9]|[1-2][0-9]|30|31)";
	var d1 = "([1-9]|[1-2][0-9]|30|31)";
	var di = -1;
	
	var H2 = "([0-1][0-9]|20|21|22|23)";
	var H1 = "([0-9]|1[0-9]|20|21|22|23)";
	var Hi = -1;

	var m2 = "([0-5][0-9])";
	var m1 = "([0-9]|[1-5][0-9])";
	var mi = -1;
	
	var s2 = "([0-5][0-9])";
	var s1 = "([0-9]|[1-5][0-9])";
	var si = -1;
	
	var regexp;
	
	function sgetDate(dateString, formatString){
	  if(validateDate(dateString, formatString)) {
		var now = new Date();
		var vals = regexp.exec(dateString);
		var index = validateIndex(formatString);
		var year = index[0]>=0?vals[index[0] + 1]:now.getFullYear();
		var month = index[1]>=0?(vals[index[1] + 1]-1):now.getMonth();
		var day = index[2]>=0?vals[index[2] + 1]:now.getDate();
		var hour = index[3]>=0?vals[index[3] + 1]:"";
		var minute = index[4]>=0?vals[index[4] + 1]:"";
		var second = index[5]>=0?vals[index[5] + 1]:"";
	
		var validate;
		if (hour == "")
		  validate = new Date(year, month, day);
		else
		  validate = new Date(year, month, day, hour, minute, second);
	
		if(validate.getDate()==day) 
			return validate;
		else
			alert('Undefined Date.');
	  }
	}


	function validateDate(dateString, formatString){
	  var dateString = trim(dateString);
	  if(dateString=="") return;
	  var reg = formatString;
	  reg = reg.replace(/yyyy/, y4);
	  reg = reg.replace(/yy/, y2);
	  reg = reg.replace(/MM/, M2);
	  reg =	reg.replace(/M/, M1);
	  reg =	reg.replace(/dd/, d2);
	  reg =	reg.replace(/d/, d1);
	  reg =	reg.replace(/HH/, H2);
	  reg =	reg.replace(/H/, H1);
	  reg =	reg.replace(/mm/, m2);
	  reg =	reg.replace(/m/, m1);
	  reg =	reg.replace(/ss/, s2);
	  reg =	reg.replace(/s/, s1);
	  reg =	new RegExp("^"+reg+"$");
	  regexp = reg;
	  return reg.test(dateString);
	}

	function validateIndex(formatString) {
	  var ia = new Array();
	  var i = 0;
	  yi = formatString.search(/yyyy/);
	  if (yi < 0) yi = formatString.search(/yy/);
	  if (yi >= 0) {
		ia[i] = yi;
		i++;
	  }
	
	  Mi = formatString.search(/MM/);
	  if ( Mi < 0 ) Mi = formatString.search(/M/);
	  if (Mi >= 0) {
		ia[i] = Mi;
		i++;
	  }
	
	  di = formatString.search(/dd/);
	  if ( di < 0 ) di = formatString.search(/d/);
	  if (di >= 0) {
		ia[i] = di;
		i++;
	  }
	
	  Hi = formatString.search(/HH/);
	  if ( Hi < 0 ) Hi = formatString.search(/H/);
	  if (Hi >= 0) {
		ia[i] = Hi;
		i++;
	  }
	
	  mi = formatString.search(/mm/);
	  if ( mi < 0 ) mi = formatString.search(/m/);
	  if (mi >= 0) {
		ia[i] = mi;
		i++;
	  }
	
	  si = formatString.search(/ss/);
	  if ( si < 0 ) si = formatString.search(/s/);
	  if (si >= 0) {
		ia[i] = si;
		i++;
	  }
	
	  var ia2 = new Array(yi, Mi, di, Hi, mi, si);
	
	  for(i=0; i<ia.length-1; i++) {
		for(j=0;j<ia.length-1-i;j++) { 
			if(ia[j]>ia[j+1]) {
				temp=ia[j]; 
				ia[j]=ia[j+1]; 
				ia[j+1]=temp;
			}
		}
	  }
	
	  for (i=0; i<ia.length ; i++) {
		for (j=0; j<ia2.length; j++) {
		  if(ia[i]==ia2[j]) {
			ia2[j] = i;
		  }
		}
	  }
	  
	  return ia2;
	}


	function trim(str){
		return str.replace(/(^\s*)|(\s*$)/g, "");
	}
	
	var dateStart=new Date() // Today
	dateStart.setHours(0,0,0,0)
	function dateOutOfRange(date) {
		var dateSelected=new Date(date);
		dateSelected.setHours(0,0,0,0);

		if (dateSelected < dateStart)
			// out of range, not allowed
			return null;

	}
	
	function cekDate() {
		var formatter = "yyyy-MM-dd HH:mm:ss";
		var currDate = sgetDate(trim(document.getElementById("<?php echo $EVENT_DATE_BEGIN; ?>").value),formatter);
		var nextDate = sgetDate(document.getElementById('<?php echo $EVENT_DATE_END; ?>').value,formatter);
		
		if (currDate >= nextDate) {
			alert("<?php echo $EVENT_DATE_ERROR; ?>");
			document.getElementById('<?php echo $EVENT_DATE_END; ?>').value = "";
		}
	}
	Zapatec.Calendar.setup({
		inputField     :    "<?php echo $EVENT_DATE_BEGIN; ?>",   // id of the input field
		button         :    "tgl1",  // What will trigger the popup of the calendar
		ifFormat       :    "%Y-%m-%d %H:%M:00",       // format of the input field
		timeFormat     :    "24",
		showsTime      :    true,          //no time
		disableFunc    :    dateOutOfRange, //the function to call
		onUpdate       :    cekDate
	});
	Zapatec.Calendar.setup({
		inputField     :    "<?php echo $EVENT_DATE_END; ?>",   // id of the input field
		button         :    "tgl2",  // What will trigger the popup of the calendar
		ifFormat       :    "%Y-%m-%d %H:%M:00",       // format of the input field
		timeFormat     :    "24",
		showsTime      :    true,          //no time
		disableFunc    :    dateOutOfRange, //the function to call
		onUpdate       :    cekDate
	});
</script>
<?php
	} else {
		echo $queryData->cari_form($EVENT_SEARCH);
		$caridata = $queryData->cari_padaku("judul","isi");
		if (isset($_SESSION['isroot'])) {
			$sql1 = "SELECT * FROM tbl_content WHERE tipe='event' ".$caridata." ORDER BY tanggal DESC LIMIT ".$limitdown.",10";	
		} else {
			$sql1 = "SELECT * FROM tbl_content WHERE tipe='event' ".$caridata." AND author='".strtolower($_SESSION['author'])."' ORDER BY tanggal DESC LIMIT ".$limitdown.",10";	
		}
?>
<table width="550" border="0" align="center" cellpadding="4" cellspacing="2" class="imagesSUB">
  <tr> 
    <th height="30" class="imagesSUB"><div align="center"><?php echo $EVENT_TITLE; ?></div></th>
    <th width="100" class="imagesSUB"><div align="center"><?php echo $EVENT_DATE_BEGIN; ?></div></th>
    <th width="100" class="imagesSUB"><div align="center"><?php echo $EVENT_DATE_END; ?></div></th>
    <th nowrap="nowrap" class="imagesSUB"><div align="center"><?php echo $EVENT_PUBLISH; ?></div></th>
    <th width="12%" class="imagesSUB"><div align="center"><?php echo $DEV_ACTION; ?></div></th>
  </tr>
<?php	
		//echo $sql1;
		$qry1 = mysql_query($sql1);
		$numrow = mysql_num_rows($qry1);
		$num = 1;
		$x = 1;
		if ($numrow==0) {
			echo '<tr><th colspan="7">'.$DEV_NO_DATA.'</th></tr>';
		} else {
			while ($row1 = mysql_fetch_object($qry1)) {
				$data = $x % 2;
				if ($data==0) {
					$dataX = "ganjil";
				} else {
					$dataX = "genap";
				} 
				$eventData = explode("[break]",$row1->header_c);
?>
  <tr valign="top" class="<?php echo $dataX; ?>" onmouseover="setBG('bg<?php echo $x; ?>','#FFFFDD')" onmouseout="setBG('bg<?php echo $x; ?>','')" id="bg<?php echo $x; ?>"> 
    <td><div align="left"><?php echo $row1->judul; ?></div></td>
    <td nowrap="nowrap"><div align="left"><?php echo $queryData->tanggalan($eventData[0],false,false,true,true);?></div></td>
    <td nowrap="nowrap"><div align="left"><?php echo $queryData->tanggalan($eventData[1],false, false, true, true);?></div></td>
    <td><div align="center"><?php (($row1->status)==1) ? print "<img src=\"images/ico_check.gif\" width=\"16\" height=\"16\"></div></td>" : print "<img src=\"images/ico_delete.gif\" width=\"16\" height=\"16\"></div></td>"; ?></div></td>
<?php 
		echo $queryData->ctrl_data('','index.php?show='.SHOW.'&showview='.SHOWVIEW.'&act=edit&kode='.$row1->idx,$path.'ver_event.php?show='.SHOW.'&amp;showview='.SHOWVIEW.'&amp;kode='.$row1->idx.'&amp;act_ver=Hapus'); ?>
  </tr>
<?php
				$num++;
				$x++;
			}
		}
?>
</table>
<div align="center"><?php echo $queryData->next_prev($num,$pages_syarat,$limitdown); ?></div>
<?php
	}
?>